﻿@namespace BlazorFluentUI.Routing
@inherits FluentUIComponentBase
@using System.Linq

@if (Direction == LayoutDirection.Horizontal)
{
    <ResizeGroup OnGrowData=@onGrowData OnReduceData=@onReduceData
                    Data=@_currentData
                    GetCacheKey=@(x => x?.CacheKey)
                    ClassName=@ClassName
                    Style=@Style>
        <DataTemplate Context="data">
            <div class="mediumFont ms-NavBar-horizontal" role="menubar">
                <div style="margin:8px 15px 0 -15px;">
                    @HeaderTemplate(Header!)
                </div>
                @*Primary*@
                <OverflowSet ClassName="ms-NavBar-horizontal-primarySet" Items=@data!.PrimaryItems OverflowItems=@(data.OverflowItems!.Count() > 0 ? data.OverflowItems : null) GetKey=@(x=>x.Key)>
                    <ItemTemplate>
                        @if (context.IconOnly)
                        {
                            <TooltipHost>
                                <ChildContent>
                                    <CommandBarButton IconName=@context.IconName
                                                         IconSrc=@context.IconSrc
                                                         Text=@(!context.IconOnly ? context.Text : null)
                                                         MenuItems=@context.Items
                                                         Href=@context.Url
                                                         Disabled=@context.Disabled
                                                         AriaLabel=@context.AriaLabel
                                                         Checked=@context.Checked
                                                         ClassName=@(context.ClassName + (context.Items?.Concat(Items.Where(x => x.Items != null).SelectMany(x => GetChild(x.Items!))).FirstOrDefault(x=>x.Checked==true) != null ? $" subgroup-is-checked":""))
                                                         Split=@context.Split Style=@context.Style
                                                         Command=@context.Command
                                                         CommandParameter=@context.CommandParameter/>
                                </ChildContent>
                                <TooltipContent>
                                    @context.Text
                                </TooltipContent>
                            </TooltipHost>
                        }
                        else
                        {
                            <CommandBarButton IconName=@context.IconName
                                                 IconSrc=@context.IconSrc
                                                 Text=@(!context.IconOnly ? context.Text : null)
                                                 MenuItems=@context.Items
                                                 Href=@context.Url
                                                 Disabled=@context.Disabled
                                                 AriaLabel=@context.AriaLabel
                                                 Checked=@context.Checked
                                                 ClassName=@(context.ClassName + (context.Items?.Concat(Items.Where(x => x.Items != null).SelectMany(x => GetChild(x.Items!))).FirstOrDefault(x=>x.Checked==true) != null ? $" subgroup-is-checked":""))
                                                 Split=@context.Split Style=@context.Style
                                                 Command=@context.Command
                                                 CommandParameter=@context.CommandParameter/>
                        }
                    </ItemTemplate>
                    <OverflowTemplate>
                        <CommandBarButton HideChevron="true" Style="min-width: 0; padding: 0 4px; align-self: stretch;" IconName="More" MenuItems=@context />
                    </OverflowTemplate>

                </OverflowSet>
                <div class="ms-NavBar-horizontal-secondarySet">
                    @FooterTemplate
                </div>
            </div>
        </DataTemplate>
    </ResizeGroup>
}
else
{
    <Stack>
        <Nav>
            <NavLinkGroup GroupHeaderTemplate=@HeaderTemplate
                             Name=@Header
                             CollapseByDefault="false">
                <ChildContent>
                    @foreach (var item in Items)
                    {
                        <RecursiveNavLink Name=@item.Text Url=@item.Url NavMatchType=@item.NavMatchType Id=@item.Id Items=@(item.Items) IconName=@item.IconName IconSrc=@item.IconSrc
                                             @bind-IsExpanded=@item.IsExpanded @bind-IsExpanded:event="IsExpandedChanged"
                                             Command=@item.Command CommandParameter=@item.CommandParameter/>
                    }
                    @foreach (var item in OverflowItems)
                    {
                        <RecursiveNavLink Name=@item.Text Url=@item.Url NavMatchType=@item.NavMatchType Id=@item.Id Items=@(item.Items) IconName=@item.IconName IconSrc=@item.IconSrc
                                             @bind-IsExpanded=@item.IsExpanded @bind-IsExpanded:event="IsExpandedChanged"
                                             Command=@item.Command CommandParameter=@item.CommandParameter/>
                    }
                </ChildContent>
            </NavLinkGroup>

        </Nav>
        <StackItem Shrink="0">
            @FooterTemplate
        </StackItem>
    </Stack>
}

@code{
    [Parameter]
    public RenderFragment<string> HeaderTemplate { get; set; } = (header) =>@<div style="font-size:17px;font-weight:400;">@header</div>;

}